from .ai import AI
class Battle:
    def __init__ (self, player_pokemon, enemy_pokemon):
        self.player_pokemon = player_pokemon
        self.enemy_pokemon = enemy_pokemon
        self.current_turn = None
        self.battle_log = []
        self.is_battle_over = False
        self.winner = None
        self.current_action = None
        self.is_escape = False  # 新增逃跑标记
        self.ai = AI()

    def start_battle(self):
        self.battle_log.append(f"fight start!{self.player_pokemon.name} vs {self.enemy_pokemon.name}")
        self.determine_first_move()

    def determine_first_move(self):
        if self.player_pokemon.speed >= self.enemy_pokemon.speed:
            self.current_turn = "player"
        else:
            self.current_turn = "enemy"

    def player_turn(self, action_type, skill_index=None):
        if self.current_turn != "player" or self.is_battle_over:
            return False

        if action_type == "skill" and skill_index is not None:
            success, message = self.player_pokemon.use_skill(skill_index,self.player_pokemon, self.enemy_pokemon)
            self.battle_log.append(message)
        
            if self.enemy_pokemon.is_fainted:
                self.end_battle("player")
                return True
            
        self.current_turn = "enemy"
        self.enemy_turn()
        return True

    def enemy_turn(self):
        if self.is_battle_over:
            return
        if self.enemy_pokemon.ai_level == "easy_ai":
            skill_index = self.ai.easy_ai(self.enemy_pokemon)
        elif self.enemy_pokemon.ai_level == "medium_ai":
            skill_index = self.ai.medium_ai(self.enemy_pokemon, self.player_pokemon)
        elif self.enemy_pokemon.ai_level == "hard_ai":
            skill_index = self.ai.hard_ai(self.enemy_pokemon, self.player_pokemon)

        success, message = self.enemy_pokemon.use_skill(skill_index, self.enemy_pokemon,self.player_pokemon)
        self.battle_log.append(message)
        if self.player_pokemon.is_fainted:
            self.end_battle("enemy")
            return

        self.current_turn = "player"

    def end_battle(self, winner):
        print(f"battle end! {winner} wins!") #test for check
        self.is_battle_over = True
        self.winner = self.player_pokemon if winner == "player" else self.enemy_pokemon
        self.battle_log.append(f"fight end! {winner} wins!")
        #heal player's and enemy's hp
        self.player_pokemon.current_hp = self.player_pokemon.max_hp
        self.enemy_pokemon.current_hp = self.enemy_pokemon.max_hp 
        #player get enemy's equipment
        if winner == "player":
            self.player_pokemon.get_enemy_equip(self.enemy_pokemon)
            print(f"get enemy's equipment success!") #test for check
            player_level = self.player_pokemon.level
            enemy_level = self.enemy_pokemon.level
            if player_level == enemy_level:
                self.player_pokemon.level += 1
                print(f"player level up to {self.player_pokemon.level}!") #test for check
        #heal player's and enemy's skills pp
        for skill in self.player_pokemon.skills_deck:
            skill.current_pp = skill.max_pp
        for skill in self.enemy_pokemon.skills_deck:
            skill.current_pp = skill.max_pp
        self.player_pokemon.is_fainted = False
        self.enemy_pokemon.is_fainted = False

    def get_battle_log(self):
        return self.battle_log 